import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <P>
 *     作用: 测试本章中sevlet容器应用程序
 * </P>
 * @author 大忽悠
 * @create 2022/3/5 22:43
 */
public class PrimitiveServlet implements Servlet {

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("servlet初始化中....");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("service方法执行中....");
        //返回的writer已经开启了自动刷新功能---针对println方法
        PrintWriter writer = servletResponse.getWriter();
        StringBuilder builder=new StringBuilder();
        String string = builder.append("HTTP/1.1 200 OK\r\n")
                .append("Content-Type:text/html;charset=UTF-8\r\n")
                .append("\r\n")
                .toString();
        String parameter = servletRequest.getParameter("name");
        writer.println(string);
        writer.println("<h1>测试servlet返回结果,请求参数name对应的值为: "+parameter+" </h1>");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("servlet销毁中....");
    }
}
